<?php
require_once( 'test_header.php');
require_once( CLASSMATE_ROOT.'/include/functions/mongo_connect.function.php' );
session::create();

class test_class_user_auth_process extends UnitTestCase{

	 private $db_connect;

	 function setUp(){
	 	 
		 //建立数据库连接,并选择测试数据库
		 $this->db_connect = mongo_connect( '172.17.0.46' , 'test' );
		 //选择或建立测试需要的数据表
	 	 $collection = $this->db_connect->user;		 
		 //存入测试必须的数据
		 $obj = array( "username" => "test", "password" => "123" );
	 	 $collection->insert($obj);

	 }
	 function tearDown(){
	 }

	 //测试用户成功登录的情况
	 function test_user_do_login_success(){

	 	 //mock 用户的输入
	 	 $request = array( 'username' => 'test' , 'password' => '123' );
		 $user_auth_process = new user_auth_process( $this->db_connect );

		 $login_result = $user_auth_process->do_login( $request );
		 $this->assertTrue( $login_result );

		 //测试系统对于用户登录状态的感知即
	 	 //对于已登录用户,调用do_login()返回false
		 //$login_result = $user_auth_process->do_login( $request );
		 //$this->assertFalse( $login_result );

		 //退出登录
		 $logout_result= $user_auth_process->do_logout();
		 $this->assertTrue( $logout_result );

		 //退出登录之后has_login()必返回false
		 //$has_login_result = $user_auth_process->has_login( $username );
	 	 //$this->assertFalse( $has_login_result );
	 }
	 //测试用户提供的用户名及密码不匹配的情况
	 function test_user_do_login_fail(){
	 	 
		 //mock 用户的输入
		 $request = array( 'username' => 'there_is_no_this_user' , 'password' => 'whatever' );
		 $user_auth_process = new user_auth_process( $this->db_connect );

		 $this->expectException();
		 $user_auth_process->do_login( $request );
	 }
	 
}

$test = new test_class_user_auth_process();
$test->run( new HtmlReporter() );
